﻿#region

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

#endregion

namespace DataAccessLayer
{
    public static class BusinessCustomerDAL
    {
        private static readonly string ConnectionString =
            ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        /// <summary>
        ///     Get All business customers
        /// </summary>
        /// <returns>All business customers</returns>
        public static List<BusinessCustomer> GetAllPersonalCustomers()
        {
            List<BusinessCustomer> customers = new List<BusinessCustomer>();
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand("spGetAllBusinessCustomers", connection)
                {
                    CommandType = CommandType.StoredProcedure
                };

                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        BusinessCustomer customer = new BusinessCustomer
                        {
                            CustomerId = Convert.ToInt32(reader["CustomerId"]),
                            CompanyName = reader["CompanyName"].ToString(),
                            EMail = reader["EMail"].ToString(),
                            TelephoneNo = reader["TelephoneNo"].ToString(),
                            Type = reader["Type"].ToString(),
                            Username = reader["Username"].ToString()
                        };

                        if (reader["SubsidiaryId"] != DBNull.Value)
                        {
                            customer.SubsidiaryId = Convert.ToByte(reader["SubsidiaryId"]);
                        }

                        customers.Add(customer);
                    }
                }
            }
            return customers;
        }

        public static void RegisterBusinessCustomer(BusinessCustomer customer, User user)
        {
            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
            {
                con.Open();
                var cmd = new SqlCommand("spRegisterBusinessCustomer", con) {CommandType = CommandType.StoredProcedure};
                var nameParameter = new SqlParameter("@CompanyName", customer.CompanyName);
                var emailParameter = new SqlParameter("@Email", customer.EMail);
                var telParameter = new SqlParameter("@Telephone", customer.TelephoneNo);
                var typeParameter = new SqlParameter("@Type", customer.Type);
                var unameParameter = new SqlParameter("@Username", user.Username);
                var passParameter = new SqlParameter("@Password", user.Password);
                var subsidiaryParameter = new SqlParameter("@SubsidiaryId", customer.SubsidiaryId)
                {
                    SqlDbType = SqlDbType.TinyInt
                };
                cmd.Parameters.Add(nameParameter);
                cmd.Parameters.Add(emailParameter);
                cmd.Parameters.Add(telParameter);
                cmd.Parameters.Add(typeParameter);
                cmd.Parameters.Add(unameParameter);
                cmd.Parameters.Add(passParameter);
                cmd.Parameters.Add(subsidiaryParameter);
                cmd.ExecuteNonQuery();
            }
        }

        public static void UpdateBusinessCustomer(BusinessCustomer customer)
        {
            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
            {
                con.Open();
                var cmd = new SqlCommand("spUpdateBusinessCustomer", con) {CommandType = CommandType.StoredProcedure};
                var customerIdParameter = new SqlParameter("@CustomerId", customer.CustomerId)
                {
                    SqlDbType = SqlDbType.Int
                };
                var nameParameter = new SqlParameter("@CompanyName", customer.CompanyName);
                var emailParameter = new SqlParameter("@Email", customer.EMail);
                var telParameter = new SqlParameter("@Telephone", customer.TelephoneNo);
                var subsidiaryParameter = new SqlParameter("@SubsidiaryId", customer.SubsidiaryId)
                {
                    SqlDbType = SqlDbType.TinyInt
                };
                cmd.Parameters.Add(customerIdParameter);
                cmd.Parameters.Add(nameParameter);
                cmd.Parameters.Add(emailParameter);
                cmd.Parameters.Add(telParameter);
                cmd.Parameters.Add(subsidiaryParameter);
                cmd.ExecuteNonQuery();
            }
        }

        public static void GetBusinessCustomerDetails(BusinessCustomer customer)
        {
            using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString))
            {
                con.Open();
                var cmd = new SqlCommand("spGetBusinessCustomerDetails", con)
                {
                    CommandType = CommandType.StoredProcedure
                };
                var customerIdParameter = new SqlParameter("@CustomerId", customer.CustomerId);
                cmd.Parameters.Add(customerIdParameter);
                var rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    customer.CompanyName = rdr["CompanyName"].ToString();
                    customer.EMail = rdr["EMail"].ToString();
                    customer.TelephoneNo = rdr["TelephoneNo"].ToString();
                    customer.Type = rdr["Type"].ToString();
                    customer.Username = rdr["Username"].ToString();
                    customer.SubsidiaryId = (string.IsNullOrEmpty(rdr["SubsidiaryId"].ToString())
                        ? Convert.ToByte(5)
                        : (byte) rdr["SubsidiaryId"]);
                }
            }
        }
    }
}